
package com.wyzz.hr.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IPositionBaseBo;
import com.wyzz.hr.bo.IPositionBaseHistBo;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.pojo.PositionBaseHist;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.SysConfigManager;


public class OrgMapAction extends BaseAction {

	public OrgMapAction() {
		deptList = new ArrayList();
		selectDepthList = new ArrayList();
		dbConfigManager = DatabaseSysConfigManager.getInstance();
	}

	public String showOrgMap() {
		Map dbMap = dbConfigManager.getProperties();
		haveHist = (String) dbMap.get("sys.profile.save.history");
		AssembleDeptList assembler = AssebleDeptListFactory.creator(queryDate,
				queryDeptId, queryDepth);
		deptList = assembler.getList();
		queryDeptId = assembler.getQueryDeptId();
		treeDepth = assembler.getTreeDepth().intValue();
		setSelectDepthList();
		return "success";
	}

	private void setSelectDepthList() {
		for (int i = 2; i <= treeDepth; i++)
			selectDepthList.add(Integer.valueOf(i));

	}

	public List getPbsByDept(String deptId, String queryDate) {
		List pbList = new ArrayList();
		IPositionBaseBo positionBaseBo = (IPositionBaseBo) SpringBeanFactory
				.getBean("positionBaseBo");
		if (queryDate == null) {
			pbList = positionBaseBo.getActivePbsByDept(deptId);
		} else {
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date queryDateForPb;
			try {
				queryDateForPb = dateFormat.parse(queryDate);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
			IPositionBaseHistBo pbhBo = (IPositionBaseHistBo) SpringBeanFactory
					.getBean("positionBaseHistBo");
			List pbhList = pbhBo.getRequirePbhList(queryDateForPb, deptId);
			for (int i = 0; i < pbhList.size(); i++) {
				PositionBaseHist pbh = (PositionBaseHist) pbhList.get(i);
				pbList.add(positionBaseBo.loadPb(pbh.getPbhiPbId().getId(),
						new String[] { PositionBase.PROP_PB_DEPT_ID }));
			}

		}
		return pbList;
	}

	public Date getQueryDate() {
		return queryDate;
	}

	public void setQueryDate(Date queryDate) {
		this.queryDate = queryDate;
	}

	public String getQueryDeptId() {
		return queryDeptId;
	}

	public void setQueryDeptId(String queryDeptId) {
		this.queryDeptId = queryDeptId;
	}

	public Integer getQueryDepth() {
		return queryDepth;
	}

	public void setQueryDepth(Integer queryDepth) {
		this.queryDepth = queryDepth;
	}

	public List getDeptList() {
		return deptList;
	}

	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}

	public List getSelectDepthList() {
		return selectDepthList;
	}

	public void setSelectDepthList(List selectDepthList) {
		this.selectDepthList = selectDepthList;
	}

	public String getHaveHist() {
		return haveHist;
	}

	public void setHaveHist(String haveHist) {
		this.haveHist = haveHist;
	}

	private static final long serialVersionUID = 1L;
	private Date queryDate;
	private String queryDeptId;
	private Integer queryDepth;
	private String haveHist;
	private List deptList;
	private List selectDepthList;
	private int treeDepth;
	private SysConfigManager dbConfigManager;
}
